package com.wanggsx.library.base.mvp;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;

import com.wanggsx.library.base.app.needextends.SuperAppInstance;
import com.wanggsx.library.util.UtilsStatusBarNavigationBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by {wanggsx} on 2017/4/19
 */

public abstract class BaseActivity extends AppCompatActivity{
    private View statusBarView = null;//
    private Unbinder unbinder;//ButterKnife
    protected View mRootView;
    private int mOriginPaddingTop;

    protected boolean getIsToPaddingStatusBarHeight(){
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setDimAmount(0);
        super.onCreate(savedInstanceState);
        setContentCiew();
        if (mRootView!=null) {
            mOriginPaddingTop = mRootView.getPaddingTop();
            resetStatusBarHight();
        }
        unbinder = ButterKnife.bind(this);
        setPresenter();
        initData(savedInstanceState);
    }

    protected  void resetStatusBarHight(){
        if (getIsToPaddingStatusBarHeight()) {
            mRootView.setPadding(mRootView.getPaddingLeft(),mOriginPaddingTop + UtilsStatusBarNavigationBar.getStatusBarHeight(this),
                    mRootView.getPaddingRight(),mRootView.getPaddingBottom());
        }else{
            mRootView.setPadding(mRootView.getPaddingLeft(),mOriginPaddingTop,
                    mRootView.getPaddingRight(),mRootView.getPaddingBottom());
        }
        UtilsStatusBarNavigationBar.setTextColorNotTransparentStatusBar(this,setStatusBarColor());
    }

    /**
     * 可以返回颜色名：white
     * 可以返回颜色值：#ffffff
     * */
    protected String setStatusBarColor(){
        return "#ffffff";
    }

    protected void setPresenter() {
    }

    /** 延迟加载时间,单位毫秒 */
    protected int lazyLoadViewDelayTime(){
        return 0;
    }

    /**
     * 设置
     */
    private void setContentCiew() {
        SuperAppInstance.mListActivitys.add(this);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (setLayoutResourceID() != 0) {
            mRootView = LayoutInflater.from(this).inflate(setLayoutResourceID(),null);
            if (lazyLoadViewDelayTime()>0){
                //延迟显示
                mRootView.setVisibility(View.INVISIBLE);
                setContentView(mRootView);
                mRootView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                             mRootView.setVisibility(View.VISIBLE);
                    }
                },lazyLoadViewDelayTime());
            }else{
                setContentView(mRootView);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBusMessage message) {
        busMessage(message);
    }


    protected abstract int setLayoutResourceID();

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract void busMessage(EventBusMessage message);

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        if (unbinder!=null){
            unbinder.unbind();
            unbinder = null;
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        SuperAppInstance.mListActivitys.remove(this);
        super.onDestroy();
    }
}
